/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "LOGR_paths.h"
#include "LOGR_webstat.h"
#include "LOGR_gen_links.h"

WebPathPtr
new_web_path_node(void)
{
  WebPathPtr upn = (WebPathPtr)calloc(1, sizeof(WebPathStruct));
  if(!upn) {
      fprintf(stderr,"Out of Memory new_web_path_node\n");
      abort();
  }
  upn->ledges = init_linked_list();
  upn->naccesses = 1;
  return upn;
}

void
delete_web_path_node(WebPathPtr upn)
{	
    if(!upn) return;
    if(upn->ledges)
	Delete_all_list(upn->ledges); 
    free(upn); upn = NULL;
}

void
LOGR_set_path_naccesses(WebPathPtr upn,long num)
{
  if(!upn) return;
  upn->naccesses = num;
}

void
LOGR_inc_path_naccesses(WebPathPtr upn)
{
  if(!upn) return;
  upn->naccesses++;
}

long
LOGR_get_path_naccesses(WebPathPtr upn)
{
  if(!upn) return 0;
  return upn->naccesses;
}

void
LOGR_set_path_info(WebPathPtr upn,void * info)
{
  if(!upn) return;
  upn->info = info;
}

void *
LOGR_get_path_info(WebPathPtr upn)
{
  if(!upn) return 0;
  return upn->info;
}

void
LOGR_set_path_ledges(WebPathPtr upn,LNode_type * ledges)
{
  if(!upn) return;
  upn->ledges = ledges;
}

void
LOGR_add_path_edge(WebPathPtr upn,void *edge)
{
  if(!upn) return;
  if(upn->ledges)
      Insert_linked_list(upn->ledges,create_new_node_llist((char *)edge));   
}

LNode_type *
LOGR_get_path_ledges(WebPathPtr upn)
{
  if(!upn) return 0;
  return upn->ledges;
}

int
LOGR_get_path_number_edges(WebPathPtr upn)
{
  if(!upn) return 0;
  return (upn->ledges ? count_llist(upn->ledges) : 0);
}

short
LOGR_is_paths_equal(WebPathPtr upn,WebPathPtr vpn)
{
  LNode_type *gptr = NULL;
  LNode_type *hptr = NULL;
  LNode_type *glp = NULL;
  LNode_type *hlp = NULL;
  int sg=0,sh =0;

  if(!upn || !vpn) return 0;

  glp = upn->ledges;
  hlp = vpn->ledges;

  sg = count_llist(glp);
  sh = count_llist(hlp);

  if(sg != sh)
      return 0;

  for(gptr = glp->back, hptr = hlp->back; gptr != glp; 
      gptr = gptr->back,hptr = hptr->back)
  {
      WebGenLinkPtr glink = (WebGenLinkPtr)(gptr->info);
      WebGenLinkPtr hlink = (WebGenLinkPtr)(hptr->info);
      if(!LOGR_is_gen_links_equal(glink,hlink))
	  return 0;
  }
  return 1; 
}

short
LOGR_is_path_empty(WebPathPtr wpn)
{
    if(!wpn)
	return 1;
    if(is_empty_list(wpn->ledges))
	return 1;
    return 0;
}


int
LOGR_is_path_number_edges_lower(WebPathPtr u, WebPathPtr v)
{
  int cu = LOGR_get_path_number_edges(u);
  int cv = LOGR_get_path_number_edges(v);
  return (cu < cv);
}

int
LOGR_is_path_number_edges_bigger(WebPathPtr u,WebPathPtr v)
{
  int cu = LOGR_get_path_number_edges(u);
  int cv = LOGR_get_path_number_edges(v);
  return (cu > cv);  
}

